const express = require("express");
const { uploading } = require("../utils/tool");
const multer = require("multer");
const { UploadError } = require("../utils/errors");
const formatRsponse = require("../utils/responseFormat");
const uploadRouter = express.Router();

uploadRouter.post("/", (req, res, next) => {
  uploading.single("file")(req, res, (err) => {
    if (err instanceof multer.MulterError) {
      next(new UploadError("文件上传失败，请检查文件大小，控制在2M以内"));
    } else {
      const path = "/static/uploads/" + req.file.filename;

      res.send(formatRsponse(0, "", path));
    }
  });
});

module.exports = uploadRouter;
